﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Effects.Effects
{
    /// <summary>
    ///  Event arguments for changing overlap factor in <see cref="PitchShift"/> class.
    /// </summary>
    public class OverlapEventArgs : EventArgs
    {
        /// <summary>
        ///  Current overlap.
        /// </summary>
        private int overlap;

        /// <summary>
        ///  Gets overlap.
        /// </summary>
        public int Overlap
        {
            get { return overlap; }
        }

        /// <summary>
        ///  Creates new instance representing given overlap factor.
        /// </summary>
        /// <param name="overlap"> Overlap factor. </param>
        public OverlapEventArgs(int overlap)
        {
            this.overlap = overlap;
        }
    }

    /// <summary>
    ///  Delegate for handling events that sets new overlap factor.
    /// </summary>
    /// <param name="sender"> Object that has raised the event. </param>
    /// <param name="e"> Event arguments. </param>
    public delegate void NewOverlapFactor(object sender,OverlapEventArgs e);

    /// <summary>
    ///  Simple form that allows user set overlap factor in <see cref="global::Effects.Effects.PitchShift"/> class. 
    /// </summary>
    /// <remarks>
    ///   It contains just one textbox and two buttons - OK and Cancel. What can be simplier? :)
    /// </remarks>
    public partial class OverlapFactorForm : Form
    {
        /// <summary>
        ///  Creates new OverlapFactorForm
        /// </summary>
        public OverlapFactorForm()
        {
            InitializeComponent();
        }

        /// <summary>
        ///  Handler for OK button click. Raises the <see cref="OverlapFactorForm.OnOverlapChange"/> event with new value of overlap 
        ///  and closes the form.
        /// </summary>
        /// <param name="sender"> OK button </param>
        /// <param name="e"> Event arguments. </param>
        private void OkButton_Click(object sender, EventArgs e)
        {
            int overlap=0;
            if(int.TryParse(OverlapTextBox.Text,out overlap))
            {

                if (overlap == 1 || overlap == 2 || overlap == 4 || overlap == 8 || overlap == 16 || overlap == 32)
                {
                    OverlapEventArgs OEArgs = new OverlapEventArgs(overlap);
                    try
                    {
                        this.OnOverlapChange(this, OEArgs);
                    }
                    catch (NullReferenceException) { }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Overlap factor must be power of 2 between 1 and 32");
                }
            }
            else
            {
                MessageBox.Show("Overlap factor must be an integer");
            }
        }

        /// <summary>
        ///  Cancel button handler. Closes the form.
        /// </summary>
        /// <param name="sender"> Cancel button </param>
        /// <param name="e"> Event arguments. </param>
        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        ///  Sets overlap displayed by form.
        /// </summary>
        /// <param name="overlap"></param>
        public void SetOverlap(int overlap)
        {
            if (overlap == 1 || overlap == 2 || overlap == 4 || overlap == 8 || overlap == 16 || overlap == 32)
            {
                this.OverlapTextBox.Text = overlap.ToString();
            }
        }

        /// <summary>
        /// Raised when user confirms the overlap factor change.
        /// </summary>
        public event NewOverlapFactor OnOverlapChange;
    }
}
